package com.openecommerce.core.util;

import java.util.Calendar;
import java.util.Date;

import com.openecommerce.core.exception.SystemException;



/**
 * 时间日期工具类
 * 
 * @author Tommy
 * 
 */
public class DateTimeUtils {

	/**
	 * 两date比较
	 * 
	 * @param beforeDate
	 * @param afterDate
	 * @return
	 */
	public static int compareDate(Date beforeDate, Date afterDate) {
		Calendar beforeCalendar = Calendar.getInstance();
		Calendar afterCalendar = Calendar.getInstance();
		beforeCalendar.setTime(beforeDate);
		afterCalendar.setTime(afterDate);
		return beforeCalendar.compareTo(afterCalendar);
	}
	
	/**
	 * 判断目标日期是否在时间段类
	 * @param beforeDate
	 * @param afterDate
	 * @param targetDate
	 * @return
	 */
	public static boolean isBetweenDate(Date beforeDate, Date afterDate,Date targetDate){
		if(targetDate == null){
			throw new SystemException("targetDate should not be null!");
		}
		if(beforeDate == null && afterDate == null){
			return false;
		}
		if(afterDate == null){
			return (compareDate(beforeDate,targetDate) <= 0);
		}
		if(beforeDate == null){
			return (compareDate(targetDate,afterDate)<= 0);
		}
		return (compareDate(beforeDate,targetDate) <= 0)&&(compareDate(targetDate,afterDate)<= 0);
	}

}
